package com.xz.xzutils.module.xzmybatislogplus;

import com.github.vertical_blank.sqlformatter.SqlFormatter;
import com.github.vertical_blank.sqlformatter.core.FormatConfig;
import com.xz.xzutils.utils.MySqlFormatter;

import java.util.regex.Pattern;

/**
 * @author DONG.WANG17@HAND-CHINA.COM
 * @since 2025/2/27-02-27 23:07
 */

public class TEst {
    static String sql = """
            select wss.id, wss.status_code,
            
            
            
            
             wsst.status_name, wss.status_classify, 
            wss.enabled_flag, wss.is_preset, wss.remark, wss.tenant_id, wss.created_by, wss.last_updated_by, wss.creation_date, wss.last_update_date, wss.object_version_number, wss.attribute1, wss.attribute2, wss.attribute3, wss.attribute4, wss.attribute5, wss.attribute6, wss.attribute7, wss.attribute8, wss.attribute9, wss.attribute10, wss.attribute11, wss.attribute12, wss.attribute13, wss.attribute14, wss.attribute15 FROM wmdm_stock_status wss INNER JOIN wmdm_stock_status_tl wsst ON wsst.id = wss.id AND wsst.lang = 'zh_CN' WHERE wss.tenant_id IN (0, 2) AND wss.enabled_flag = 1 AND name='xxx\n\nxxx\n' ORDER BY wss.is_preset DESC, wss.enabled_flag DESC, wss.status_code
            
            
            """;

    public static void main(String[] args) throws Exception {
        sqlFormatter();
        System.out.println("=======================================");
        // sqlFormatter2();
    }

    public static void sqlFormatter() {
        String indent = "    ";
        FormatConfig formatConfig = FormatConfig.builder()
                .indent(indent)
                .uppercase(false)
                .linesBetweenQueries(-1)
                .skipWhitespaceNearBlockParentheses(true)
                .build();
        String format = SqlFormatter.format(sql, formatConfig);
        System.out.println(format);
        System.out.println("-------------------------------------");
        System.out.println(MySqlFormatter.compressed(sql));
    }

    // 正则表达式替换换行符,压缩代码快
    public static void sqlFormatter2() {
        String pattern = "('(?:[^']|'')*')|[\r\n]+";
        String result = Pattern.compile(pattern)
                .matcher(sql)
                .replaceAll(mr -> mr.group(1) != null ? mr.group(1) : " ");
        System.out.println(result);
    }
}
